package com.jason.config;

import com.jason.util.ClassUtil;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;

public class XMLMapperBuilder {

    private final String CONTRACT_SIGN = ".";

    private Configuration configuration;

    public XMLMapperBuilder(Configuration configuration) {
        this.configuration = configuration;
    }

    /**
     * 解析mapper.xml文件
     * @param inputStream
     */
    public void parse(InputStream inputStream) throws DocumentException, ClassNotFoundException {
        Document document = new SAXReader().read(inputStream);
        Element root = document.getRootElement();
        String namespace = root.attributeValue("namespace");
        parseSelectElement(root, namespace);
        parseInsertElement(root, namespace);
        parseUpdateElement(root, namespace);
        parseDeleteElement(root, namespace);
    }

    /**
     * 解析select标签
     * @param root
     * @param namespace
     * @throws ClassNotFoundException
     */
    private void parseSelectElement(Element root, String namespace) throws ClassNotFoundException {
        List<Element> selectElments = root.selectNodes("//" + MapperStatement.SELECT);
        for (Element element : selectElments) {
            String id = element.attributeValue("id");
            String parameterType = element.attributeValue("parameterType");
            String resultType = element.attributeValue("resultType");
            String sql = element.getTextTrim();
            //封装mapperStatement
            MapperStatement mapperStatement = new MapperStatement();
            mapperStatement.setOperation(MapperStatement.SELECT);
            mapperStatement.setId(id);
            mapperStatement.setResultType(ClassUtil.getClassType(resultType));
            mapperStatement.setParameterType(ClassUtil.getClassType(parameterType));
            mapperStatement.setSql(sql);
            String statementId = namespace + CONTRACT_SIGN + id;
            this.configuration.getMapperStatementMap().put(statementId, mapperStatement);
        }
    }

    /**
     * 解析insert标签
     * @param root
     * @param namespace
     * @throws ClassNotFoundException
     */
    private void parseInsertElement(Element root, String namespace) throws ClassNotFoundException {
        List<Element> insertElements = root.selectNodes("//" + MapperStatement.INSERT);
        for (Element element : insertElements) {
            String id = element.attributeValue("id");
            String parameterType = element.attributeValue("parameterType");
            String sql = element.getTextTrim();

            MapperStatement mapperStatement = new MapperStatement();
            mapperStatement.setOperation(MapperStatement.INSERT);
            mapperStatement.setSql(sql);
            mapperStatement.setId(id);
            mapperStatement.setParameterType(ClassUtil.getClassType(parameterType));
            this.configuration.getMapperStatementMap().put(namespace + CONTRACT_SIGN + id, mapperStatement);
        }
    }

    /**
     * 解析update标签
     * @param root
     * @param namespace
     * @throws ClassNotFoundException
     */
    private void parseUpdateElement(Element root, String namespace) throws ClassNotFoundException {
        List<Element> updateElements = root.selectNodes("//" + MapperStatement.UPDATE);
        for (Element element : updateElements) {
            String sql = element.getTextTrim();
            String id = element.attributeValue("id");
            String parameterType = element.attributeValue("parameterType");

            MapperStatement mapperStatement = new MapperStatement();
            mapperStatement.setOperation(MapperStatement.UPDATE);
            mapperStatement.setId(id);
            mapperStatement.setSql(sql);
            mapperStatement.setParameterType(ClassUtil.getClassType(parameterType));
            String statementId = namespace + CONTRACT_SIGN + id;
            this.configuration.getMapperStatementMap().put(statementId, mapperStatement);
        }
    }

    /**
     * 解析delete标签
     * @param root
     * @param namespace
     * @throws ClassNotFoundException
     */
    private void parseDeleteElement(Element root, String namespace) throws ClassNotFoundException {
        List<Element> deleteElements = root.selectNodes("//" + MapperStatement.DELETE);
        for (Element element : deleteElements) {
            String id = element.attributeValue("id");
            String parameterType = element.attributeValue("parameterType");
            String sql = element.getTextTrim();
            String statementId = namespace + CONTRACT_SIGN + id;

            MapperStatement mapperStatement = new MapperStatement();
            mapperStatement.setOperation(MapperStatement.DELETE);
            mapperStatement.setSql(sql);
            mapperStatement.setId(id);
            mapperStatement.setParameterType(ClassUtil.getClassType(parameterType));
            this.configuration.getMapperStatementMap().put(statementId, mapperStatement);
        }
    }

}
